# coding=utf-8
"""used for server hide db key
"""

import json
from Crypto.Cipher import AES
from Crypto import Random
import binascii
import base64
import uuid

def get_mac_address(): 
    mac=uuid.UUID(int = uuid.getnode()).hex[-12:] 
    return ":".join([mac[e:e+2] for e in range(0,11,2)])


def padding_aes(str_plain):
	fs = str_plain + '1'
	x = len(fs) % 16
	if x != 0:
		fs_pad = fs + '0'*(16 - x)
	return fs_pad

def depadding_aes(str_pad):
	found=False
	for i in xrange(1,16):
		if str_pad[-i] == '0':
			continue
		if str_pad[-i] == '1':
			found=True
		break

	if found:
		return str_pad[0:-i]
	else:
		return str_pad
	


print "----decode ---"
verify_data_code="ak47"

verify_data_str_encoded = "MlIFz1wrWYTBpJ9F07ZCBvsH+XvZWntFXbcAKGG0LGlv+DGASQ+JQg7GmkYCkplLED6DAcTAj5xzOovpmksT36rP7FyFffwt7aWjWxjnGxOgba9L3BHa6xNrd44XZ0v8iJjG1O0oyEl2mwvCXAAQwo4cXr9TX4Mxh9dj/MramWA="

print "----decode work ---"

def get_db_sec_code(verify_data_code,verify_data_str_encoded):
	mac_ip=get_mac_address()+''

	mode=AES.MODE_CBC
	verify_data_code_aes=AES.new(padding_aes(verify_data_code), mode,padding_aes(verify_data_code))


	verify_data_str = verify_data_code_aes.decrypt(base64.b64decode(verify_data_str_encoded))
	verify_data_str = depadding_aes(verify_data_str)
	#print verify_data_str
	
	try:
		verify_data=json.loads(verify_data_str)
		mac_ip_iv = base64.b64decode(verify_data["mac_ip_iv"])
		mac_ip_aes=AES.new(padding_aes(mac_ip), mode,mac_ip_iv)
		db_sec_code_mac_ip=verify_data["db_sec_code_mac_ip"]
		db_sec_code = depadding_aes(mac_ip_aes.decrypt(base64.b64decode(db_sec_code_mac_ip)))
		if db_sec_code[:2] == 'ok' and __name__.endswith (verify_data['mod_name']):
			verify_pass=True
			db_sec_code=db_sec_code[2:]
		else:
			db_sec_code="bbs_acdTY123"
			verify_pass=False
	except:
		db_sec_code="bbs_acdTY123"
		verify_pass=False

	return verify_pass,db_sec_code


#cant be seen by out
verify_pass,AES.__db_sec_code__=get_db_sec_code(verify_data_code,verify_data_str_encoded)
print verify_pass,AES.__db_sec_code__,len(AES.__db_sec_code__)

print "----work ---"
db_sec_code="bbs_acdTY123"
#print db_sec_code

print "cant be used as main"
print __name__

